package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.bean.DataBean;
import com.example.config.MoreTransaction;
import com.example.mapper.one.DataOneMapper;
import com.example.mapper.two.DataTwoMapper;
import com.example.service.DataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DataServiceImpl implements DataService {

    @Autowired
    public DataServiceImpl(DataOneMapper oneMapper, DataTwoMapper twoMapper) {
        this.oneMapper = oneMapper;
        this.twoMapper = twoMapper;
    }

    DataOneMapper oneMapper;
    DataTwoMapper twoMapper;

    @Override
    @MoreTransaction(value = {"oneTransactionManager", "twoTransactionManager"})
    public boolean process(int num) {
        int insert;

        DataBean dataBean = new DataBean();
        dataBean.setData("test");
        insert = oneMapper.insert(dataBean);

        int div = 1 / num;
        System.out.println(div);

        DataBean dataBean2 = new DataBean();
        dataBean2.setData("test");
        insert = twoMapper.insert(dataBean2);

        return true;
    }

    @Override
    public String addOneData(String name) {
        DataBean dataBean = new DataBean();
        dataBean.setData(name);
        int insert = oneMapper.insert(dataBean);
        return insert > 0 ? "插入成功" : "插入失败";
    }

    @Override
    public List<DataBean> getTwoData() {
        return twoMapper.selectList(new QueryWrapper<DataBean>());
    }
}
